package com.example.xlmkotlinwanandroid.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.xlmkotlinwanandroid.R
import com.example.xlmkotlinwanandroid.model.bean.ArticleBean
import com.example.xlmkotlinwanandroid.ui.activity.WebActivity

class ProjectAdapter(var context: Context?) : RecyclerView.Adapter<ProjectAdapter.ViewHolder>() {

    private val inflater: LayoutInflater = LayoutInflater.from(context)
    private var dataList = ArrayList<ArticleBean>()

    fun setData(datas: List<ArticleBean>) {
       val subList = datas as ArrayList<ArticleBean>
        dataList.addAll(subList)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = inflater.inflate(R.layout.item_project, parent, false)
        return ViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val projectArticleBean = dataList[position]
        holder.item_project_author.text = projectArticleBean.author
//        holder.item_project_top.text = "未知"
//        holder.item_project_new.text = projectArticleBean.author
//        holder.item_project_type1.text = projectArticleBean.author
        holder.item_project_date.text = projectArticleBean.niceDate
        //设置照片
        Glide.with(holder.item_project_imageview).load(projectArticleBean.envelopePic)
            .into(holder.item_project_imageview)
        holder.item_project_title.text = projectArticleBean.title
        holder.item_project_content.text = projectArticleBean.desc
        holder.item_project_type.text = "${projectArticleBean.superChapterName}"
        //绑定点击事件
        holder.itemView.setOnClickListener {
            val intent = Intent(context, WebActivity::class.java)
            intent.putExtra("url", projectArticleBean?.link)
            intent.putExtra("title", projectArticleBean?.title)
            context?.startActivity(intent)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val item_project_toplinear: LinearLayout =
            itemView.findViewById(R.id.item_project_toplinear)
        val item_project_author: TextView = itemView.findViewById(R.id.item_project_author)
        val item_project_top: TextView = itemView.findViewById(R.id.item_project_top)
        val item_project_new: TextView = itemView.findViewById(R.id.item_project_new)
        val item_project_type1: TextView = itemView.findViewById(R.id.item_project_type1)
        val item_project_date: TextView = itemView.findViewById(R.id.item_project_date)
        val item_project_imageview: ImageView = itemView.findViewById(R.id.item_project_imageview)
        val item_project_title: TextView = itemView.findViewById(R.id.item_project_title)
        val item_project_content: TextView = itemView.findViewById(R.id.item_project_content)
        val item_project_type: TextView = itemView.findViewById(R.id.item_project_type)
    }
}